﻿using HealthcareSystem.API.Write.Application.Command.TeamCommand;
using HealthcareSystem.Domain.BasicSettings.Team;
using MediatR;
using WeekExam.PassPort.ErrorCode;
using WeekExam.PassPort.Infrastructure.Interface;

namespace HealthcareSystem.API.Write.Application.CommandHandel.TeamCommandHandler
{
    /// <summary>
    /// 团队状态（启用、停用）请求处理
    /// </summary>
    public class TeamStateCommandHandler : IRequestHandler<TeamStateCommand, ApiResult<int>>
    {
        private readonly IBaseRepository<TeamTable> teamRep;

        public TeamStateCommandHandler(IBaseRepository<TeamTable> teamRep)
        {
            this.teamRep = teamRep;
        }

        public async Task<ApiResult<int>> Handle(TeamStateCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> result=new ApiResult<int>();
            result.Code=ResultCode.Success;
            result.Message=ResultCode.查询成功.ToString();
            
            var res=teamRep.GetAll().FirstOrDefault(x=>x.TeamId==request.TeamId);
            if (res.TeamStatus == true)
            {
                res.TeamStatus = false;
            }
            else
            {
                res.TeamStatus=true;
            }
            await teamRep.UpdAsync(res);
            return await Task.FromResult(result);   


        }
    }
}
